package com.fudan.backend.dao;

import com.fudan.backend.handler.ListTypeHandler;
import com.fudan.backend.handler.IdTypeHandler;
import com.fudan.backend.pojo.Store;
import com.fudan.backend.pojo.vo.StoreVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface StoreDao {
    @Results(id = "storeResultMap", value = {
            @Result(column = "store_id", property = "storeId", typeHandler = IdTypeHandler.class),
            @Result(column = "user_id", property = "userId", typeHandler = IdTypeHandler.class),
            @Result(column = "store_name", property = "storeName"),
            @Result(column = "category", property = "category", typeHandler = ListTypeHandler.class),
            @Result(column = "id_card_number", property = "idCardNumber"),
            @Result(column = "store_profile", property = "storeProfile"),
            @Result(column = "filing_address", property = "filingAddress"),
            @Result(column = "registered_capital", property = "registeredCapital"),
            @Result(column = "registration_date", property = "registrationDate"),
            @Result(column = "pictures", property = "pictures", typeHandler = ListTypeHandler.class),
            @Result(column = "activity_id", property = "activityId", typeHandler = IdTypeHandler.class),
            @Result(column = "account_id", property = "accountId", typeHandler = IdTypeHandler.class),
            @Result(column = "status", property = "status"),
    })
    @Select("select * from t_store")
    List<Store> selectAllStore();

    @ResultMap("storeResultMap")
    @Select("select * from t_store where store_id = #{storeId}")
    Store selectStoreByStoreId(String storeId);


    @ResultMap("storeResultMap")
    @Select("select * from t_store where store_name = #{storeName}")
    Store selectStoreByStoreName(String storeName);

    @Select("select store_id from t_store where activity_id = #{activityId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    List<String> selectStoreIdByActivityId(String activityId);

    @Results(id = "storeIdResultMap", value = {
            @Result(column = "store_id", property = "storeId", typeHandler = IdTypeHandler.class),
    })
    @Select("select store_id from t_store where user_id = #{userId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    List<String> selectAllStoreIdByUserId(String userId);


    @Results(id = "storeVoResultMap", value = {
            @Result(column = "store_id", property = "storeId", typeHandler = IdTypeHandler.class),
            @Result(column = "store_name", property = "storeName"),
            @Result(column = "category", property = "category", typeHandler = ListTypeHandler.class),
            @Result(column = "store_profile", property = "storeProfile"),
            @Result(column = "filing_address", property = "filingAddress"),
            @Result(column = "registered_capital", property = "registeredCapital"),
            @Result(column = "registration_date", property = "registrationDate"),
            @Result(column = "activity_id", property = "activityId", typeHandler = IdTypeHandler.class),
            @Result(column = "pictures", property = "pictures", typeHandler = ListTypeHandler.class),
            @Result(column = "status", property = "status"),
    })
    @Select("select * from t_store where store_id = #{storeId} and status = 1")
    StoreVo selectStoreVoByStoreId(String storeId);

    @ResultMap("storeVoResultMap")
    @Select("select * from t_store where status = 1")
    List<StoreVo> selectAllStoreVo();

    @Insert("insert into t_store values "+
            "(#{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, #{userId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{storeName}, #{category, typeHandler=com.fudan.backend.handler.ListTypeHandler}, " +
            "#{idCardNumber}, #{storeProfile}, #{filingAddress}, #{registeredCapital}, #{registrationDate}, " +
            "#{pictures, typeHandler=com.fudan.backend.handler.ListTypeHandler}, " +
            "#{activityId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{accountId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, #{status})")
    int insertStore(Store store);

    @Delete("delete from t_store where store_id = #{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    int deleteStoreByStoreId(String storeId);

    @Update("update t_store set "+
            "user_id = #{userId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, store_name = #{storeName}, " +
            "category = #{category, typeHandler=com.fudan.backend.handler.ListTypeHandler}, id_card_number = " +
            "#{idCardNumber}, "+
            "store_profile = #{storeProfile}, filing_address = #{filingAddress}, registered_capital = #{registeredCapital}, "+
            "registration_date = #{registrationDate}, pictures = #{pictures, typeHandler=com.fudan.backend.handler.ListTypeHandler}, " +
            "activity_id = #{activityId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "account_id = #{accountId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, status = #{status} "+
            "where store_id = #{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    int updateStore(Store store);

    @ResultMap("storeResultMap")
    @Select("select * from t_store " +
            "where user_id = #{userId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    List<Store> selectStoreByUserId(String userId);
}
